package zj.xinxin.business.business.redis;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import zj.xinxin.api.moudle.redis.RedisCacheBusiness;
import zj.xinxin.api.moudle.system.dto.DictBackDto;
import zj.xinxin.api.moudle.system.entity.DictEntity;
import zj.xinxin.common.constant.CommonConst;
import zj.xinxin.common.constant.RedisConst;
import zj.xinxin.common.core.redis.RedisCache;
import zj.xinxin.common.utils.AssertUtil;
import zj.xinxin.common.utils.BeanCopyUtil;
import zj.xinxin.mapper.mapper.system.DictEntityMapper;

import java.util.List;

/**
 * @author junzhou
 * @date 2022/3/23 10:56
 * @since 1.8
 */
@Service
public class RedisCacheBusinessImpl implements RedisCacheBusiness {

    @Autowired
    private RedisCache redisCache;

    @Autowired
    private DictEntityMapper dictEntityMapper;

    @Override
    public void cacheIconList() {
        /*------------------------  检查redis中是否含有数据开始  -----------------------------*/
        List<DictBackDto> dictCache = redisCache.getCacheList(RedisConst.DICT_ICON_LIST);
        if (AssertUtil.isNotEmpty(dictCache)) {
            return;
        }
        /*------------------------  检查redis中是否含有数据完成  -----------------------------*/
        /*------------------------  开始查询图标信息  -----------------------------*/
        DictEntity dict = dictEntityMapper.selectOne(new LambdaQueryWrapper<DictEntity>()
                .eq(DictEntity::getDictCode, CommonConst.DICT_ICON_CODE));
        List<DictEntity> dicts = dictEntityMapper.selectList(new LambdaQueryWrapper<DictEntity>()
                .eq(DictEntity::getParentId, dict.getDictId()));
        /*------------------------  查询图标信息完成  -----------------------------*/
        /*------------------------  存储图标信息开始  -----------------------------*/
        redisCache.setCacheList(RedisConst.DICT_ICON_LIST, BeanCopyUtil.copyList(dicts, DictBackDto.class));
        /*------------------------  存储图标信息完成  -----------------------------*/

    }
}
